package com.wanpinghui.supplier.user.order.adapter;


import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wanpinghui.supplier.R;
import com.wanpinghui.wphlibrary.model.order.bean.OrderItemBean;
import com.yunshl.yunshllibrary.recyclerview.BaseRecyclerViewAdapter;
import com.yunshl.yunshllibrary.recyclerview.OnItemClickListener;
import com.yunshl.yunshllibrary.utils.TimeUtil;

/**
 * 功能说明：
 * Created by caitiangui
 * 创建时间： 2017/12/21.
 */

public class OrderAdapter extends BaseRecyclerViewAdapter<OrderItemBean, OrderAdapter.OrderViewHolder> {


    private setOnItemListener listener;


    //private OrderGoodsAdapter adapter;

    public OrderAdapter(Context context) {
        super(context);
        //adapter = new OrderGoodsAdapter(context);
    }

    @Override
    public OrderViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_order_list, parent, false);
        return new OrderViewHolder(view);
    }

    @Override
    public int getItemCount() {
        return datas == null ? 0 : datas.size();
    }

    @Override
    public void onBindViewHolder(OrderViewHolder holder, int position) {
        super.onBindViewHolder(holder, position);
        final OrderItemBean bean = datas.get(position);
        holder.tv_name.setText("采购人： " + bean.getBuyer_name() + "   " + bean.getSafeBuyerMobile());
        holder.tv_order_code.setText("订单编号：" + bean.getOrder_no());
        holder.tv_order_time.setText(TimeUtil.format(bean.getCreated_at() * 1000, "yyyy-MM-dd HH:mm"));
        if (bean.getRemarkList() != null && bean.getRemarkList().size() > 0) {
            holder.tv_status.setText(bean.getRemarkList().get(0).getRemark());
        } else {
            holder.tv_status.setText("暂无");
        }
        OrderGoodsAdapter adapter = new OrderGoodsAdapter(context);
        holder.recyclerview.setLayoutManager(new LinearLayoutManager(context));
        holder.recyclerview.setAdapter(adapter);
        adapter.setDatas(bean.getItemList());
        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                if (listener != null) {
                    listener.onClick(bean);
                }
            }
        });
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onClick(bean);
                }
            }
        });
    }


    public void setListener(setOnItemListener listener) {
        this.listener = listener;
    }

    class OrderViewHolder extends RecyclerView.ViewHolder {

        TextView tv_name;

        RecyclerView recyclerview;
        LinearLayout root_view;
        TextView tv_order_code;
        TextView tv_order_time;
        TextView tv_status;

        OrderViewHolder(View itemView) {
            super(itemView);
            tv_name = (TextView) itemView.findViewById(R.id.tv_name);
            tv_order_time = (TextView) itemView.findViewById(R.id.tv_order_time);
            tv_status = (TextView) itemView.findViewById(R.id.tv_status);
            tv_order_code = (TextView) itemView.findViewById(R.id.tv_order_code);
            recyclerview = (RecyclerView) itemView.findViewById(R.id.recyclerview);
            root_view = (LinearLayout) itemView.findViewById(R.id.root_view);
        }
    }

    public interface setOnItemListener {
        void onClick(OrderItemBean bean);
    }


}
